// 计算总金额
function total() {
	setTimeout(function () {
		var t = 0;
		$.each($('.weui-panel__ft'), function () {
			var $checkbox_bd = $(this).prev('.weui-panel__bd').find("input[type='checkbox']");
			var s = 0;
			var n = 0;
			$.each($(this).prev('.weui-panel__bd').find(".weui-count__number"), function (i) {
				if ($checkbox_bd.eq(i).attr("checked") == "checked") {
					s = s + parseInt($(this).html()) * parseFloat($(this).parents('.weui-media-box__bd').find('.price').html().replace("¥", ""));
					n = n + parseInt($(this).html())
				}
			});
			$(this).find("span").html("¥" + s.toFixed(2));
			$(this).find("number").html(n);
			t = t + s
		});
		$(".cart-footer .total span").html("¥" + t.toFixed(2))
	}, 100)
}
// 判断有无数据
function hide() {
	if ($(".item").length == 0) {
		$(".cart-footer").hide();
		$(".no-cart").css("display", "block");
		return
	} else {
		$(".cart-footer").show();
		$(".no-cart").css("display", "none")
	}
}
// 判断是否全选
function sum() {
	if ($(".weui-panel__bd input[checked='checked']").length == $(".weui-media-box").length) {
		$(".cart-footer input[type=checkbox]").attr("checked", "checked");
		$(".cart-footer input[type=checkbox]").next(".iconfont").attr("class", "iconfont icon-yuanxingxuanzhongfill")
	} else {
		$(".cart-footer input[type=checkbox]").removeAttr("checked");
		$(".cart-footer input[type=checkbox]").next(".iconfont").attr("class", "iconfont icon-yuanxingweixuanzhong")
	}
}
// checkbox状态
function checkbox($this) {
	if ($this.attr('type') == "checkbox") {
		if ($this.attr('checked') == "checked") {
			$this.removeAttr("checked");
			$this.next(".iconfont").attr("class", "iconfont icon-yuanxingweixuanzhong")
		} else {
			$this.attr("checked", "checked");
			$this.next(".iconfont").attr("class", "iconfont icon-yuanxingxuanzhongfill")
		}
	}
	total()
}
$(function () {
	hide();
	total();
	// 编辑
	var flag = 0;
	$("#edit").click(function () {
		if (flag === 0) {
			$(".cart-footer #del").show();
			$(".cart-footer #account").hide();
			$(".cart-footer .total").hide();
			flag = 1;
		} else {
			$(".cart-footer #del").hide();
			$(".cart-footer #account").show();
			$(".cart-footer .total").show();
			flag = 0;
		}
		hide()
	});
	// 全选
	$('.cart-footer .hd input').change(function () {
		if ($(this).attr("checked") == "checked") {
			$(".cart input[type='checkbox']").removeAttr("checked");
			$(".cart input[type='checkbox']").next(".iconfont").attr("class", "iconfont icon-yuanxingweixuanzhong")
		} else {
			$(".cart input[type='checkbox']").attr("checked", "checked");
			$(".cart input[type='checkbox']").next(".iconfont").attr("class", "iconfont icon-yuanxingxuanzhongfill")
		}
		checkbox($(this))
	});
	// 监听商店
	$('.weui-panel__hd input').change(function () {
		var $checkbox_bd = $(this).parents('.weui-panel__hd').next('.weui-panel__bd').find('input[type=checkbox]');
		if ($(this).attr("checked") == undefined) {
			$checkbox_bd.attr("checked", "checked");
			$checkbox_bd.next(".iconfont").attr("class", "iconfont icon-yuanxingxuanzhongfill")
		} else {
			$checkbox_bd.removeAttr("checked");
			$checkbox_bd.next(".iconfont").attr("class", "iconfont icon-yuanxingweixuanzhong")
		}
		checkbox($(this));
		sum()
	});
	// 监听商品
	$(".weui-panel__bd input[type='checkbox']").change(function () {
		checkbox($(this));
		var $checkbox_hd = $(this).parents('.weui-panel__bd').prev('.weui-panel__hd').find('input');
		if ($(this).parents('.weui-panel__bd').find("input[checked='checked']").length == $(this).parents(".weui-panel__bd").children('.weui-media-box').length) {
			$checkbox_hd.attr("checked", "checked");
			$checkbox_hd.next(".iconfont").attr("class", "iconfont icon-yuanxingxuanzhongfill")
		} else {
			$checkbox_hd.removeAttr("checked");
			$checkbox_hd.next(".iconfont").attr("class", "iconfont icon-yuanxingweixuanzhong")
		}
		sum()
	});
	// 加数量
	$('.weui-count__increase').click(function () {
		$(this).prev('.weui-count__number').html(parseInt($(this).prev('.weui-count__number').html()) + 1);
		total()
	});
	// 减数量
	$('.weui-count__decrease').click(function () {
		if ($(this).next('.weui-count__number').html() == 0) {
			$(this).next('.weui-count__number').html(0);
		} else {
			$(this).next('.weui-count__number').html(parseInt($(this).next('.weui-count__number').html()) - 1);
		}
		total()
	});
	// 删除
	$('#del').click(function () {
		$.each($('.weui-media-box'), function () {
			if ($(this).find("input[type=checkbox]").attr("checked") == "checked") {
				$(this).remove()
			}
		});
		$('input[type=checkbox]').attr("checked", "checked");
		$('input[type=checkbox]').next(".iconfont").attr("class", "iconfont icon-yuanxingxuanzhongfill");
		$.each($(".item"), function () {
			if ($(this).find(".weui-media-box").length == 0) {
				$(this).remove()
			}
		});
		hide();
		total()
	})
})

